/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "KMsgFw11Q.h"
#include <QFile>
#include <QSettings>
#include <QSslError>
#include <QSslSocket>
#include <QStringList>
#include <QHostAddress>
#include <QSslCertificate>

KMsgFw11Q::KMsgFw11Q(QObject *parent) :
    QObject(parent)
{
    tcpSocket = new QSslSocket();
#ifdef WIN32_SHARED
    QList<QSslCertificate> cert = QSslCertificate::fromPath(QLatin1String(":/interface.msg.keppe.org.pem"));
#else
    QList<QSslCertificate> cert = QSslCertificate::fromData("-----BEGIN CERTIFICATE-----\nMIIHXTCCBkWgAwIBAgIDEB00MA0GCSqGSIb3DQEBBQUAMIGMMQswCQYDVQQGEwJJ\nTDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERpZ2l0\nYWwgQ2VydGlmaWNhdGUgU2lnbmluZzE4MDYGA1UEAxMvU3RhcnRDb20gQ2xhc3Mg\nMSBQcmltYXJ5IEludGVybWVkaWF0ZSBTZXJ2ZXIgQ0EwHhcNMTQwNDI2MDAyOTM5\nWhcNMTUwNDI2MTE1NDQ4WjBqMRkwFwYDVQQNExBQOVdCQWlKOHFjbzI5ZUwyMQsw\nCQYDVQQGEwJERTEgMB4GA1UEAxMXaW50ZXJmYWNlLm1zZy5rZXBwZS5vcmcxHjAc\nBgkqhkiG9w0BCQEWD2FkbWluQGtlcHBlLm9yZzCCAiIwDQYJKoZIhvcNAQEBBQAD\nggIPADCCAgoCggIBAPBBDUnY9ebNdQlgOQQJOSeqHYmx+2eBPY5QJ2N94KVLTvjZ\nXo5qIqDeE/wpCcrFANq3iX1i6LdlOtcvLtCgK07zSuukfyBSwTUF5VlSlH1iKiIk\nImxtIAI8LXIAkFCqljFWiytc7uQ8PM1XV3X9oH90gKJQLQMLM2rcGn2lpnzAxgUX\nOADpx+ZUnbJGeJwy4cTrqYxVgItGrln/nKPG/1dqI21UgJDF4GbPsVHWtQQUTcHx\nfyhqs0C99T58Y3PhDORctx56n0jd9eJM7A/zo3hmNNymsitz4W8YXshW9ApGRz5T\nGA6mHyV9kAIuROyEfys4HbZNcHQDU6nll44sjJHRNEZLlFE4cXVjJOgXqcK5lxJB\nYBBdbw6i4ohM/iyufeTmarAiMcUTmO/QSKMknOMfVQlBJuRy5Tf7WuWOnhZ2GK2P\n68V2tOR2tvairMac8nYlC3F7iniRzuflZPWGBO9YIHZvygau8HT6Zc4LxwNK2qrd\niBpN4HxVMJAO4KkDkpZISVvuFUahCojvi6IdmaAx+vw5yfyM+AsPcOYoQQ/m/J+p\n5tVIdhPGBJq1lMoaEGq6c6yGYl7f4eMfbgefpGmHJqZObzuZQCGrfxfWCqlFfbnR\nL1kmzjsH9kgjuJ0TLoYIt/3huoyH0rYciatxIrWfmYN5Zmd6apKbJbfgO1wtAgMB\nAAGjggLnMIIC4zAJBgNVHRMEAjAAMAsGA1UdDwQEAwIDqDATBgNVHSUEDDAKBggr\nBgEFBQcDATAdBgNVHQ4EFgQUiOdPjUzlRfUo0Xk8FG2JK81W4ewwHwYDVR0jBBgw\nFoAU60I00Jiwq5/0G2sI98xkLu8OLEUwLQYDVR0RBCYwJIIXaW50ZXJmYWNlLm1z\nZy5rZXBwZS5vcmeCCWtlcHBlLm9yZzCCAVYGA1UdIASCAU0wggFJMAgGBmeBDAEC\nATCCATsGCysGAQQBgbU3AQIDMIIBKjAuBggrBgEFBQcCARYiaHR0cDovL3d3dy5z\ndGFydHNzbC5jb20vcG9saWN5LnBkZjCB9wYIKwYBBQUHAgIwgeowJxYgU3RhcnRD\nb20gQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwAwIBARqBvlRoaXMgY2VydGlmaWNh\ndGUgd2FzIGlzc3VlZCBhY2NvcmRpbmcgdG8gdGhlIENsYXNzIDEgVmFsaWRhdGlv\nbiByZXF1aXJlbWVudHMgb2YgdGhlIFN0YXJ0Q29tIENBIHBvbGljeSwgcmVsaWFu\nY2Ugb25seSBmb3IgdGhlIGludGVuZGVkIHB1cnBvc2UgaW4gY29tcGxpYW5jZSBv\nZiB0aGUgcmVseWluZyBwYXJ0eSBvYmxpZ2F0aW9ucy4wNQYDVR0fBC4wLDAqoCig\nJoYkaHR0cDovL2NybC5zdGFydHNzbC5jb20vY3J0MS1jcmwuY3JsMIGOBggrBgEF\nBQcBAQSBgTB/MDkGCCsGAQUFBzABhi1odHRwOi8vb2NzcC5zdGFydHNzbC5jb20v\nc3ViL2NsYXNzMS9zZXJ2ZXIvY2EwQgYIKwYBBQUHMAKGNmh0dHA6Ly9haWEuc3Rh\ncnRzc2wuY29tL2NlcnRzL3N1Yi5jbGFzczEuc2VydmVyLmNhLmNydDAjBgNVHRIE\nHDAahhhodHRwOi8vd3d3LnN0YXJ0c3NsLmNvbS8wDQYJKoZIhvcNAQEFBQADggEB\nACfrlFi6Gl7CR31Vq2bAsfbYOC6oF3VJYwB/NNE/LBrfasp5tVLhLw2o1ZvErQXS\n90NMkgZ/2kHhsf6hxqGk0z4WnbaBL/7tncM7T+/goTmiZ1w7d/M2+t6z5M5IxlKc\nH1wFGBiokk1OnIXFGvrTY3bdP17hz+Oa6sRxhCX7e//5O0nigm9wAOMuJYihPL5M\nqtq4DxnH2apamS5HtyMHV2vSuvl9QePHgPjUOQ40pRpjUbNULLAmGLuROTHeHF6K\n2cGBuhAOW58XoBKMI3abqsGO3EWzBP6G2hw1bhJ0DLeFd0DsTN5GpngZRLJWjQuF\nv2SbwI+KF8SKx4S8+IhJYvE=\n-----END CERTIFICATE-----");
#endif
	if (cert.length() >= 1)
    {
    	QSslError HostNameMismatch = QSslError(QSslError::HostNameMismatch, cert.at(0));
    	QSslError SelfSignedCertificate = QSslError(QSslError::SelfSignedCertificate, cert.at(0));
    	QSslError UnableToGetLocalIssuerCertificate = QSslError(QSslError::UnableToGetLocalIssuerCertificate, cert.at(0));
    	QSslError CertificateUntrusted = QSslError(QSslError::CertificateUntrusted, cert.at(0));
    	QSslError UnableToVerifyFirstCertificate = QSslError(QSslError::UnableToVerifyFirstCertificate, cert.at(0));
    	QSslError CertificateExpired = QSslError(QSslError::CertificateExpired, cert.at(0));
    	sslErrors.append(HostNameMismatch);
    	sslErrors.append(SelfSignedCertificate);
    	sslErrors.append(UnableToGetLocalIssuerCertificate);
    	sslErrors.append(CertificateUntrusted);
    	sslErrors.append(UnableToVerifyFirstCertificate);
   		sslErrors.append(CertificateExpired);
	}
#ifdef WIN32
    QSettings rcv("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",QSettings::NativeFormat);
    system = rcv.value("ProductName","Windows").toString();
#else
#ifdef Q_WS_MAC
    system = "Mac OS X";
#else
#ifdef SYS_ANDROID
    system = "Android";
#else
    system = "Linux";
#endif
#endif
#endif
    loggedIn = false;
}

int KMsgFw11Q::loginUser(QString username, QString password)
{
    loggedIn = false;
    tcpSocket = new QSslSocket();
    tcpSocket->ignoreSslErrors(sslErrors);
    tcpSocket->connectToHostEncrypted("interface.msg.keppe.org",9506);
    bool wfe = tcpSocket->waitForEncrypted(5000);
    if (wfe)
    {
        tcpSocket->write("KM/1.1 login");
        tcpSocket->write(" --username=");
        username = username.replace("&","&amp;");
        username = username.replace(" ","&nbsp;");
        tcpSocket->write(username.toUtf8());
        tcpSocket->write(" --password=");
        password = password.replace("&","&amp;");
        password = password.replace(" ","&nbsp;");
        tcpSocket->write(password.toUtf8());
        tcpSocket->write(" --system=");
        QString csystem = system;
        csystem = csystem.replace("&","&amp;");
        csystem = csystem.replace(" ","&nbsp;");
        tcpSocket->write(csystem.toUtf8());
        tcpSocket->write(" --version=");
        QString cversion = getClassVersion();
        cversion = cversion.replace("&","&amp;");
        cversion = cversion.replace(" ","&nbsp;");
        tcpSocket->write(cversion.toUtf8());
        tcpSocket->write("\n");
        tcpSocket->flush();
        tcpSocket->waitForReadyRead(5000);
        while (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            QString description;
            QString result;
            int id = 500;
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&")).toInt();
                        }
                        if (argstr.left(9) == "--result=")
                        {
                            result = argstr.remove(0,9).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(14) == "--description=")
                        {
                            description = argstr.remove(0,14).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                }
            }
            if (id == 200)
            {
                loggedIn = true;
            }
            return id;
        }
    }
    return 500;
}

int KMsgFw11Q::logoutUser()
{
    tcpSocket->disconnectFromHost();
    loggedIn = false;
    return 0;
}

bool KMsgFw11Q::isLoggedIn()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserContacts()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=contacts\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserContactsLength()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=contactslength\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserContactsRequests()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=contactsrequests\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserContactsWithStates()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=contactswithstates\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserFriendRequests()
{
    return requestGetUserContactsRequests();
}

bool KMsgFw11Q::requestGetUserFriendRequestsLength()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=contactsrequestslength\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionAddUserContact(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 action --at=addcontact --username=" + username.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionDelUserContact(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 action --at=delcontact --username=" + username.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionRemoveUserContact(QString username)
{
    return actionDelUserContact(username);
}

bool KMsgFw11Q::requestGetAnotherUserOnlineState(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 request --rq=anotheruseronlinestate --username=" + username.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserOnlineState()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=onlinestate\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionSetUserOnlineState(int state)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 action --at=setonlinestate --state=" + QString::number(state).toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetAnotherUserMessageState(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 request --rq=anotherusermessagestate --username=" + username.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserMessageState()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=messagestate\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionSetUserMessageState(QString message)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            message = message.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 action --at=setmessagestate --message=" + message.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserNick()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=nick\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetAnotherUserNick(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 request --rq=anotherusernick --username=" + username.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionSetUserNick(QString nick)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            nick = nick.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 action --at=setnick --nick=" + nick.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserMail()
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            tcpSocket->write("KM/1.1 request --rq=usermail\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetAnotherUserMail(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 request --rq=anotherusermail --username=" + username.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionSetUserMail(QString mail, bool vfo)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            mail = mail.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 action --at=setmail --mail=" + mail.toUtf8() + " --vfo=" + QVariant(vfo).toString().toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionSetUserPassword(QString oldpassword, QString newpassword)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            oldpassword = oldpassword.replace("&","&amp;").replace(" ","&nbsp;");
            newpassword = newpassword.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 action --at=setpassword --oldpassword=" + oldpassword.toUtf8() + " --newpassword=" + newpassword.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::requestGetUserProfileInfo(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 request --rq=profileinfo --username=" + username.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionSendPokeToUser(QString username)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            tcpSocket->write("KM/1.1 action --at=sendpoke --username=" + username.toUtf8() + "\n");
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

bool KMsgFw11Q::actionSendMessageToUser(QString username, QString message, int id)
{
    if (loggedIn)
    {
        if (tcpSocket->state() == QAbstractSocket::ConnectedState)
        {
            username = username.replace("&","&amp;").replace(" ","&nbsp;");
            message = message.replace("&","&amp;").replace(" ","&nbsp;");
            if (id <= 0)
            {
                tcpSocket->write("KM/1.1 action --at=sendmessage --username=" + username.toUtf8() + " --message=" + message.toUtf8() + "\n");
            }
            else
            {
                tcpSocket->write("KM/1.1 action --at=sendmessage --username=" + username.toUtf8() + " --message=" + message.toUtf8() + " --id=" + QString::number(id).toUtf8() + "\n");
            }
            tcpSocket->flush();
            return true;
        }
    }
    return false;
}

QString KMsgFw11Q::getUserContacts()
{
    bool rq = requestGetUserContacts();
    if (rq)
    {
        QString table;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    QString users;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(8) == "--users=")
                        {
                            users = argstr.remove(0,8).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "contacts")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                    table = users.replace("<>","\n");
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return table;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

QString KMsgFw11Q::getUserContactsLength()
{
    bool rq = requestGetUserContactsLength();
    if (rq)
    {
        QString len;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    QString length;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(9) == "--length=")
                        {
                            length = argstr.remove(0,9).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "contactslength")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                    len = length;
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return len;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

QString KMsgFw11Q::getUserContactsRequests()
{
    bool rq = requestGetUserContactsRequests();
    if (rq)
    {
        QString table;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    QString users;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(8) == "--users=")
                        {
                            users = argstr.remove(0,8).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "contactsrequests")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                    table = users.replace("<>","\n");
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return table;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

QString KMsgFw11Q::getUserFriendRequests()
{
    return getUserContactsRequests();
}

QString KMsgFw11Q::getUserFriendRequestsLength()
{
    bool rq = requestGetUserFriendRequestsLength();
    if (rq)
    {
        QString len;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    QString length;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(9) == "--length=")
                        {
                            length = argstr.remove(0,9).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "contactrequestslength")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                    len = length;
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return len;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

QString KMsgFw11Q::getUserContactsWithStates()
{
    bool rq = requestGetUserContactsWithStates();
    if (rq)
    {
        QString table;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    QString users;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(8) == "--users=")
                        {
                            users = argstr.remove(0,8).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "contactswithstates")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                    table = users.replace("<>","\n");
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return table;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

int KMsgFw11Q::addUserContact(QString username)
{
    bool at = actionAddUserContact(username);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "addcontact")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

int KMsgFw11Q::delUserContact(QString username)
{
    bool at = actionDelUserContact(username);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "delcontact")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

int KMsgFw11Q::removeUserContact(QString username)
{
    return delUserContact(username);
}

int KMsgFw11Q::getAnotherUserOnlineState(QString username)
{
    bool rq = requestGetAnotherUserOnlineState(username);
    if (rq)
    {
        int state = 0;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(8) == "--state=")
                        {
                            state = QVariant(argstr.remove(0,8)).toInt();
                        }
                    }
                    if (request != "anotheruseronlinestate")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        if (id == 200)
        {
            return state;
        }
    }
    return 501;
}

int KMsgFw11Q::getUserOnlineState()
{
    bool rq = requestGetUserOnlineState();
    if (rq)
    {
        int state = 0;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(8) == "--state=")
                        {
                            state = QVariant(argstr.remove(0,8)).toInt();
                        }
                    }
                    if (request != "onlinestate")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        if (id == 200)
        {
            return state;
        }
    }
    return 501;
}

int KMsgFw11Q::setUserOnlineState(int state)
{
    bool at = actionSetUserOnlineState(state);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "setonlinestate")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

QString KMsgFw11Q::getAnotherUserMessageState(QString username)
{
    bool rq = requestGetAnotherUserMessageState(username);
    if (rq)
    {
        QString message;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(10) == "--message=")
                        {
                            message = argstr.remove(0,10).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "anotherusermessagestate")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return message;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

QString KMsgFw11Q::getUserMessageState()
{
    bool rq = requestGetUserMessageState();
    if (rq)
    {
        QString message;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(10) == "--message=")
                        {
                            message = argstr.remove(0,10).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "messagestate")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return message;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

int KMsgFw11Q::setUserMessageState(QString message)
{
    bool at = actionSetUserMessageState(message);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "setmessagestate")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

QString KMsgFw11Q::getAnotherUserNick(QString username)
{
    bool rq = requestGetAnotherUserNick(username);
    if (rq)
    {
        QString nick;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(7) == "--nick=")
                        {
                            nick = argstr.remove(0,7).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "anotherusernick")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return nick;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

QString KMsgFw11Q::getUserNick()
{
    bool rq = requestGetUserNick();
    if (rq)
    {
        QString nick;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(7) == "--nick=")
                        {
                            nick = argstr.remove(0,7).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "nick")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return nick;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

int KMsgFw11Q::setUserNick(QString nick)
{
    bool at = actionSetUserNick(nick);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "setnick")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

QString KMsgFw11Q::getAnotherUserMail(QString username)
{
    bool rq = requestGetAnotherUserMail(username);
    if (rq)
    {
        QString mail;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(7) == "--mail=")
                        {
                            mail = argstr.remove(0,7).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "anotherusermail")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return mail;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

QString KMsgFw11Q::getUserMail()
{
    bool rq = requestGetUserMail();
    if (rq)
    {
        QString mail;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(7) == "--mail=")
                        {
                            mail = argstr.remove(0,7).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "mail")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return mail;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

int KMsgFw11Q::setUserMail(QString mail, bool vfo)
{
    bool at = actionSetUserMail(mail, vfo);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "setmail")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

int KMsgFw11Q::setUserPassword(QString oldpassword, QString newpassword)
{
    bool at = actionSetUserPassword(oldpassword,newpassword);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "setpassword")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

QString KMsgFw11Q::getUserProfileInfo(QString username)
{
    bool rq = requestGetUserProfileInfo(username);
    if (rq)
    {
        QString image;
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                        if (argstr.left(8) == "--image=")
                        {
                            image = argstr.remove(0,8).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                    }
                    if (request != "profileinfo")
                    {
                        workWithObjectWithEmit(readed);
                        return "///Error 601";
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return "///Error 601";
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return "///Error 601";
            }
        }
        if (id == 200)
        {
            return image;
        }
        else
        {
            return QString("///Error " + QString::number(id));
        }
    }
    return "///Error 500";
}

int KMsgFw11Q::sendPokeToUser(QString username)
{
    bool at = actionSendPokeToUser(username);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "sendpoke")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

int KMsgFw11Q::sendMessageToUser(QString username, QString message)
{
    bool at = actionSendMessageToUser(username, message);
    if (at)
    {
        int id = 500;
        tcpSocket->waitForReadyRead(5000);
        if (tcpSocket->canReadLine())
        {
            QByteArray readed = tcpSocket->readLine().trimmed();
            QString readstr = QString::fromUtf8(readed);
            QStringList readlist = readstr.split(" ");
            if (readlist.length() >= 3)
            {
                if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
                {
                    QString action;
                    QString request;
                    foreach(QString argstr,readlist)
                    {
                        if (argstr.left(5) == "--at=")
                        {
                            action = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--rq=")
                        {
                            request = argstr.remove(0,5).replace("&nbsp;"," ").replace("&amp;","&");
                        }
                        if (argstr.left(5) == "--id=")
                        {
                            id = QVariant(argstr.remove(0,5)).toInt();
                        }
                    }
                    if (action != "sendmessage")
                    {
                        workWithObjectWithEmit(readed);
                        return 601;
                    }
                }
                else
                {
                    workWithObjectWithEmit(readed);
                    return 601;
                }
            }
            else
            {
                workWithObjectWithEmit(readed);
                return 601;
            }
        }
        return id;
    }
    return 501;
}

bool KMsgFw11Q::isObjectIncoming()
{
    return tcpSocket->canReadLine();
}

QString KMsgFw11Q::readObject()
{
    if (!tcpSocket->canReadLine())
    {
        return "";
    }
    QString ret = workWithObject(tcpSocket->readLine().trimmed());
    return ret;
}

QString KMsgFw11Q::workWithObject(QByteArray readed)
{
    QString readstr = QString::fromUtf8(readed);
    QStringList readlist = readstr.split(" ");
    if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "message")
    {
        QString username;
        QString message;
        int mid = -1;
        foreach(QString argstr, readlist)
        {
            if (argstr.left(11) == "--username=")
            {
                username = argstr.remove(0,11).replace("&nbsp;"," ").replace("&amp;","&");
            }
            if (argstr.left(10) == "--message=")
            {
                message = argstr.remove(0,10).replace("&nbsp;"," ").replace("&amp;","&");
            }
            if (argstr.left(6) == "--mid=")
            {
                mid = QVariant(argstr.remove(0,6)).toInt();
            }
        }
        if (username == "" || message == "")
        {
            return "";
        }
        return QString("MessageFromUser<>" + username + ": " + message + "\n");
        Q_UNUSED(mid);
    }
    else if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "poke")
    {
        QString username;
        foreach(QString argstr, readlist)
        {
            if (argstr.left(11) == "--username=")
            {
                username = argstr.remove(0,11).replace("&nbsp;"," ").replace("&amp;","&");
            }
        }
        if (username == "")
        {
            return "";
        }
        return QString("PokeFromUser<>" + username + "\n");
    }
    else if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "return")
    {
        QString reti;
        foreach(QString argstr, readlist)
        {
            reti.append("<>" + argstr.replace("&nbsp;"," ").replace("&amp;","&").replace("<","&lt;").replace(">","&gt;"));
        }
        return QString("ReturnIntialize" + reti);
    }
    else if (readlist.at(0) == "KM/1.1" && readlist.at(1) == "notification")
    {
        QString reti;
        foreach(QString argstr, readlist)
        {
            reti.append("<>" + argstr.replace("&nbsp;"," ").replace("&amp;","&").replace("<","&lt;").replace(">","&gt;"));
        }
        return QString("ReturnIntialize" + reti);
    }
    else
    {
        return "";
    }
}

QString KMsgFw11Q::workWithObjectWithEmit(QByteArray readed)
{
    QString ret = workWithObject(readed);
    emit objectIncomed(ret);
    return ret;
}

QString KMsgFw11Q::getClassVersion()
{
    return "1.1.0-9";
}
